@extends('layout.default')

@section('layout.content')
  <div class="row">
    <div class="col-xs-12">
      <a class="btn btn-primary" href="{{route('system.role.create')}}"><i class="ace-icon fa fa-plus"></i>角色</a>
    </div>
  </div>
  {{ csrf_field() }}
  <div class="space-4"></div>
  <div class="row">
    <div class="col-xs-12">
      <table class="table table-hover table-bordered table-striped">
        <thead>
        <tr>
          <th style="width:140px;">角色</th>
          <th>权限</th>
          <th class="align-right" style="width:110px;">操作</th>
        </tr>
        </thead>

        <tbody>
        @foreach ($roles as $key=>$role)
          <tr>
            <td>{{$role->name}}</td>
            <td>{{ str_replace(array('[',']','"'),'', json_encode($role->permissions()->pluck('title'),JSON_UNESCAPED_UNICODE) ) }}</td>
            <td class="align-right">
              <a href="{{route('system.role.edit', $role->id)}}" class="btn btn-minier btn-yellow"
                 style="margin-right:5px;">修改</a>
              <button type="button" class="btn-delete btn btn-minier btn-danger" data-id="{{$role->id}}">删除</button>
            </td>
          </tr>
        @endforeach
        </tbody>
      </table>

    </div><!-- /.col -->
  </div><!-- /.row -->

@stop

@section('javascript')
  <script type="text/javascript">
      $(function () {

          $('.btn-delete').on('click', function () {
              var that = $(this);
              var role_id = that.data('id');
              var uri = '/system/role/' + role_id;

              $.post(uri, {
                  '_token': $("input[name='_token']").attr('value'),
                  '_method': 'delete',
                  'is_submit': true
              }, function (data) {
                  console.log(data);
                  if (data.status === true && data.url !== null) {
                      $(window).attr('location', data.url);
                  } else {
                      layer.msg(data.message);
                  }

              }, 'json');

          });


      });
  </script>
@stop